#ifndef DODO_GENTONE_H
#define DODO_GENTONE_H

#include "organdef.h"
#include "wavetable.h"

//wheel_num: 91 , wave_table[92], wave_len[92], init_ppos[92]
//wheel_v[92]: levels to each wheel, controled by keyboard status and drawbar status.
//buf1, buf2, buf3,
//buf_flag: controled by playsound, to decide which buf can be written now.
//on_sig: on/off


void GenTone( int wheel_num, long *wave_table[], int wave_len[], int ppos[],
	double wheel_v[],
	int buf_size, smpl_t *buf1, smpl_t *buf2, int* buf_flag, int* gen_flag,
	int* on_sig )
{
	int i, j;
	smpl_t *buf;
	int playing = 0 ;
	double temp;
	while( *on_sig )
	{
		if(playing == *buf_flag)
			continue;

		playing = *buf_flag;
		if( playing == 1 )
		{
			buf=buf2;
			*gen_flag = 2;
		}
		else if( playing == 2)
		{
			buf=buf1;
			*gen_flag = 1;
		}
		else
			continue;

		for(i=0; i<buf_size; ++i)
		{
			temp = 0.0;
			for(j=1;j<=wheel_num; ++j)
			{
				temp += wave_table[j][ ppos[j] ] * wheel_v[j];
				ppos[j] = ( ppos[j] + 1 ) % wave_len[j];
			}
			buf[i] = temp;
		}

		if(*gen_flag==1)
            *gen_flag=2;
        else
            *gen_flag=1;

	}
}

#endif

